package com.anti.modular.bus.roomdeposit.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.anti.core.context.login.LoginContextHolder;
import com.anti.jdbc.DBUtils;
import com.anti.jdbc.support.page.PageParam;
import com.anti.jdbc.support.page.PageResult;
import com.anti.commom.core.exception.BusinessException;
import com.anti.modular.bus.roomdeposit.entity.RoomDeposit;
import com.anti.modular.bus.roomdeposit.enums.RoomDepositExceptionEnum;
import com.anti.modular.bus.roomdeposit.param.RoomDepositParam;
import com.anti.modular.bus.roomdeposit.result.LeaseDepositResult;
import com.anti.modular.bus.roomdeposit.result.RoomDepositResult;
import com.anti.modular.bus.roomdeposit.service.RoomDepositService;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 房间押金表service接口实现类
 *
 * @author anti
 * @date 2021-04-18 17:08:42
 */
@Service
public class RoomDepositServiceImpl implements RoomDepositService {
    @Resource
    DBUtils dbUtils;

    @Override
    public RoomDeposit getById(RoomDepositParam roomDepositParam){
        String sql="select * from bus_room_deposit where id =? ";
        return dbUtils.get(sql,RoomDeposit.class,roomDepositParam.getId());
    }

    @Override
    public PageResult page(PageParam pageParam,RoomDepositParam roomDepositParam) {
        StringBuilder sb=new StringBuilder("select * from bus_room_deposit where 1=1 ");
        ArrayList<Object> param = CollectionUtil.newArrayList();
        if (ObjectUtil.isNotNull(roomDepositParam)) {
            // 根据押金名稱 查询
            if (ObjectUtil.isNotEmpty(roomDepositParam.getDepositname())) {
               sb.append(" and depositname = ?");
               param.add(roomDepositParam.getDepositname());
            }
        }
        return dbUtils.query(pageParam,sb.toString(),param.toArray());
    }

    @Override
    public List<RoomDeposit> list(RoomDepositParam roomDepositParam) {
        String sql="";
        return dbUtils.query(sql,RoomDeposit.class);
    }

    @Override
    public List<RoomDepositResult> getDepositListByRoomId(Long roomId) {
        String sql="select a.* from bus_room_deposit a where a.roomid =? and a.status=0";
        List<RoomDepositResult> depositResults = dbUtils.query(sql, RoomDepositResult.class, roomId);
        for (int i = 0; i < depositResults.size(); i++) {
            depositResults.get(i).setKey(depositResults.get(i).getDepositid());
        }
        return depositResults;
    }

    @Override
    public List<LeaseDepositResult> getLeaseDepositListByLeaseId(Long id) {
        String sql="select a.* from bus_lease_deposit a where a.lease_id =? and a.status=0";
        List<LeaseDepositResult> depositResults = dbUtils.query(sql, LeaseDepositResult.class, id);
        for (int i = 0; i < depositResults.size(); i++) {
            depositResults.get(i).setKey(depositResults.get(i).getDepositid());
        }
        return depositResults;
    }

    @Override
    public void add(RoomDepositParam roomDepositParam) {
        RoomDeposit roomDeposit = new RoomDeposit();
        BeanUtil.copyProperties(roomDepositParam, roomDeposit);
        dbUtils.insert(roomDeposit);
    }

    @Override
    public void addEntity(RoomDeposit roomDeposit) {
        String s = dbUtils.insertKey(roomDeposit).toString();
        long l = Long.parseLong(s);
    }

    public void delete(RoomDepositParam roomDepositParam) {
        String sql="update bus_room_deposit set update_time=?,update_user,status=? where id =? ";
        dbUtils.execSql(sql,new Date(), LoginContextHolder.me().getSysLoginUserId(),2,roomDepositParam.getId());
    }

    @Override
    public void deleteYjByRoomId(Long roomId) {
        String sql="update bus_room_deposit set update_time=?,update_user=?,status=? where roomid =? and status =0";
        dbUtils.execSql(sql,new Date(), LoginContextHolder.me().getSysLoginUserId(),2,roomId);
    }

    @Override
    public void deleteYjByRoomIds(List<Long> roomId) {
        String sql="update bus_room_deposit set update_time=?,update_user=?,status=? where roomid not in (:ids) and status =0";
        HashMap<Object, Object> objectObjectHashMap = CollectionUtil.newHashMap();
        objectObjectHashMap.put("ids",roomId);
        dbUtils.execSql(sql,new Date(), LoginContextHolder.me().getSysLoginUserId(),2,objectObjectHashMap);
    }

    public void edit(RoomDepositParam roomDepositParam) {
        RoomDeposit roomDeposit = this.queryRoomDeposit(roomDepositParam);
        BeanUtil.copyProperties(roomDepositParam, roomDeposit);
        roomDeposit.setUpdateTime(new Date());
        roomDeposit.setUpdateUser(LoginContextHolder.me().getSysLoginUserId());
        dbUtils.update(roomDeposit);
    }

    @Override
    public RoomDeposit detail(RoomDepositParam roomDepositParam) {
        return this.queryRoomDeposit(roomDepositParam);
    }

    /**
     * 获取房间押金表
     *
     * @author anti
     * @date 2021-04-18 17:08:42
     */
    private RoomDeposit queryRoomDeposit(RoomDepositParam roomDepositParam) {
        RoomDeposit roomDeposit = this.getById(roomDepositParam);
        if (ObjectUtil.isNull(roomDeposit)) {
            throw new BusinessException(RoomDepositExceptionEnum.NOT_EXIST);
        }
        return roomDeposit;
    }


}
